package org.jeecg.modules.alipay.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.alipay.bo.PaymentBO;
import org.jeecg.modules.alipay.service.PayService;
import org.jeecg.modules.gwcjava.jiekou.entity.MlccUserInfo;
import org.jeecg.modules.gwcjava.jiekou.service.BusinessLogicService;
import org.jeecg.modules.gwcjava.jiekou.service.MlccUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 *  支付宝支付，控制器
 *
 * @author wangziyang
 * */
@Api(tags="后台支付控制器")
@RestController
@RequestMapping("/apply/pay")
@Slf4j
public class PayController {
    @Autowired
    private PayService payService;
    /**查询用户信息*/
    @Autowired
    private MlccUserInfoService userservice;
    @Autowired
    private BusinessLogicService logicService;

    /**
     * 确认支付订单
     * @param
     * @return boolean
     */
    @PostMapping(value = "/config_order")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public JSONObject post_config_order(PaymentBO datainfo) {
        System.out.println(datainfo.getOrderSn());
        //根据前端传入的值返回json数据
        JSONObject obj = new JSONObject();//返回对象
        try {
            MlccUserInfo userInfo = userservice.queryUserinfoByusername(datainfo.getToken(),3);
            if(userInfo==null){
                obj.put("code", 4);
                obj.put("msg", "token已失效，请重新登录");
                return obj;
            }
            if(datainfo.getOrderSn()==null || "".equals(datainfo.getOrderSn())){
                obj.put("code", 1);
                obj.put("msg", "订单编号不能为空");
                return obj;
            }
            if("0".equals(datainfo.getOrderType())){
                datainfo.setOrderName("面料下单");
            }else if("1".equals(datainfo.getOrderType())){
                datainfo.setOrderName("个订下单");
            }else if("2".equals(datainfo.getOrderType())){
                datainfo.setOrderName("团订下单");
            }else {
                obj.put("code", 3);
                obj.put("msg", "请传入订单类型");
                return obj;
            }
            PaymentBO paymentBO = logicService.query_paymentBODetail(datainfo.getOrderSn(),datainfo.getOrderType());
            if(paymentBO!=null){
                datainfo.setOrderMoney(paymentBO.getOrderMoney());
                datainfo.setOrderSn(paymentBO.getOrderSn());
                Object object = payService.pay(datainfo);
                obj.put("code", 0);
                obj.put("msg", "下单成功");
                obj.put("data", object);
                return obj;
            }else {
                obj.put("code", 1);
                obj.put("msg", "下单失败，请查阅参数是否正确");
                return obj;
            }
        }catch (Exception e){
            obj.put("code", 6);
            obj.put("msg", "参数错误=="+e);
            return obj;
        }
    }

    @ApiOperation("支付异步通知接口")
    @GetMapping("notify_url")
    public String notifyAlipay() {
        System.out.println("支付异步通知接口=========");
        return " a li pay notify ";
    }

    /**
     *  支付成功的回调
     * */
    @GetMapping(value = "/fallback")
    public Object fallback (HttpServletRequest request) {
        Map map = request.getParameterMap();
        System.out.println("进入了回调==="+map.get("alipay_trade_wap_pay_response"));
        System.out.println("进入了回调==="+map.get("msg"));
        System.out.println("进入了回调==="+map.get("out_trade_no"));
        return null;
    }


    /**
     * 确认支付订单
     * @param
     * @return boolean
     */
    @PostMapping(value = "/diao_zhifu")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public JSONObject diao_zhifu(PaymentBO datainfo) {
        System.out.println(datainfo.getOrderSn());
        //根据前端传入的值返回json数据
        JSONObject obj = new JSONObject();//返回对象
        try {
            MlccUserInfo userInfo = userservice.queryUserinfoByusername(datainfo.getToken(),3);
            if(userInfo==null){
                obj.put("code", 4);
                obj.put("msg", "token已失效，请重新登录");
                return obj;
            }
            if(datainfo.getOrderSn()==null || "".equals(datainfo.getOrderSn())){
                obj.put("code", 1);
                obj.put("msg", "订单编号不能为空");
                return obj;
            }
            if("0".equals(datainfo.getOrderType())){
                datainfo.setOrderName("面料下单");
            }else if("1".equals(datainfo.getOrderType())){
                datainfo.setOrderName("个订下单");
            }else if("2".equals(datainfo.getOrderType())){
                datainfo.setOrderName("团订下单");
            }else {
                obj.put("code", 3);
                obj.put("msg", "请传入订单类型");
                return obj;
            }
            PaymentBO paymentBO = logicService.query_paymentBODetail(datainfo.getOrderSn(),datainfo.getOrderType());
            if(paymentBO!=null){
                datainfo.setOrderMoney(paymentBO.getOrderMoney());
                datainfo.setOrderSn(paymentBO.getOrderSn());
                Object object = payService.pay(datainfo);
                obj.put("code", 0);
                obj.put("msg", "下单成功");
                obj.put("data", object);
                return obj;
            }else {
                obj.put("code", 1);
                obj.put("msg", "下单失败，请查阅参数是否正确");
                return obj;
            }
        }catch (Exception e){
            obj.put("code", 6);
            obj.put("msg", "参数错误=="+e);
            return obj;
        }
    }
}
